﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EventScavenger
{
    public partial class ViewManager : Form
    {
        private EventLogView selectedView = new EventLogView();
        public EventLogView SelectedView
        {
            get { return selectedView; }
            set { selectedView = value; }
        }
        private EventScavengerDAL sqlEventSource;

        public ViewManager()
        {
            InitializeComponent();
        }

        public DialogResult Show(EventScavengerDAL sqlEventSource)
        {
            this.sqlEventSource = sqlEventSource;
            return ShowDialog();
        }

        #region Form events
        private void ViewFilterManager_Load(object sender, EventArgs e)
        {
            if (Properties.Settings.Default.PredefineViewPaths != null)
            {
                Cursor = Cursors.WaitCursor;
                List<ListViewItem> listItems = new List<ListViewItem>();
                foreach (string path in Properties.Settings.Default.PredefineViewPaths)
                {
                    if (System.IO.File.Exists(path))
                    {
                        try
                        {
                            EventLogView eventLogView = EventLogView.LoadViewFromFile(path);
                            ListViewItem lvi = new ListViewItem(eventLogView.Name);
                            lvi.SubItems.Add(eventLogView.ViewPath);
                            lvi.Tag = eventLogView;
                            listItems.Add(lvi);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("Error loading view: " + path + " \r\n" + ex.ToString(), "Views", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
                lvwViews.Items.AddRange(listItems.ToArray());
                Cursor = Cursors.Default;
            }
            lvwViews_Resize(null, null);
        }

        #endregion

        #region Listview events
        private void lvwViews_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmdOK.Enabled = true;
            editToolStripMenuItem.Enabled = (lvwViews.SelectedItems.Count > 0);
            removeToolStripMenuItem.Enabled = (lvwViews.SelectedItems.Count > 0);
            toolStripButtonEdit.Enabled = (lvwViews.SelectedItems.Count > 0);
            toolStripButtonRemove.Enabled = (lvwViews.SelectedItems.Count > 0);
        } 
        private void lvwViews_DragDrop(object sender, DragEventArgs e)
        {
            Properties.Settings.Default.PredefineViewPaths = new System.Collections.Specialized.StringCollection();
            foreach (ListViewItem lvi in lvwViews.Items)
            {
                EventLogView eventLogView = (EventLogView)lvi.Tag;
                Properties.Settings.Default.PredefineViewPaths.Add(eventLogView.ViewPath);
            }
        }
        private void lvwViews_Resize(object sender, EventArgs e)
        {
            lvwViews.Columns[1].Width = lvwViews.ClientSize.Width - lvwViews.Columns[0].Width;
        }
        private void lvwViews_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
                editToolStripMenuItem_Click(null, null);
        }
        #endregion

        #region Button events
        private void cmdOK_Click(object sender, EventArgs e)
        {
            SaveList();
            if (lvwViews.SelectedItems.Count > 0)
            {
                selectedView = (EventLogView)lvwViews.SelectedItems[0].Tag;
            }
            DialogResult = DialogResult.OK;
            Close();
        } 

        #endregion

        #region Menu/Toolstrip events
        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            EventLogView newView = new EventLogView();
            ViewEditor viewEditor = new ViewEditor();
            viewEditor.SelectedView = newView;
            if (viewEditor.Show(sqlEventSource) == DialogResult.OK)
            {
                newView = viewEditor.SelectedView;
                ListViewItem lvi = new ListViewItem(newView.Name);
                lvi.SubItems.Add(newView.ViewPath);
                lvi.Tag = newView;
                lvwViews.Items.Add(lvi);
            }
        }
        private void toolStripButtonOpen_Click(object sender, EventArgs e)
        {
            if (openFileDialogImport.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    foreach (string filePath in openFileDialogImport.FileNames)
                    {
                        EventLogView importView = EventLogView.LoadViewFromFile(filePath);
                        ListViewItem lvi = new ListViewItem(importView.Name);
                        lvi.SubItems.Add(importView.ViewPath);
                        lvi.Tag = importView;
                        lvwViews.Items.Add(lvi);
                    }
                   
                    SaveList();
                    cmdOK.Enabled = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error importing view\r\n" + ex.ToString(), "Import", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lvwViews.SelectedItems.Count > 0)
            {
                ListViewItem lvi = lvwViews.SelectedItems[0];
                selectedView = (EventLogView)lvi.Tag;
                ViewEditor viewEditor = new ViewEditor();
                viewEditor.SelectedView = selectedView;
                if (viewEditor.Show(sqlEventSource) == DialogResult.OK)
                {
                    selectedView = viewEditor.SelectedView;
                    lvi.Text = selectedView.Name;
                    lvi.SubItems[1].Text = selectedView.ViewPath;
                    lvi.Tag = selectedView;
                    SaveList();
                }
            }
        }
        private void removeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to remove the selected view(s)?", "Remove", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                foreach (ListViewItem lvi in lvwViews.SelectedItems)
                {
                    lvwViews.Items.Remove(lvi);
                }
                SaveList();
                cmdOK.Enabled = true;
            }
        } 
        #endregion

        #region Private methods
        private void SaveList()
        {
            Properties.Settings.Default.PredefineViewPaths = new System.Collections.Specialized.StringCollection();
            foreach (ListViewItem lvi in lvwViews.Items)
            {
                Properties.Settings.Default.PredefineViewPaths.Add(lvi.SubItems[1].Text);
            }
            Properties.Settings.Default.Save();
        }
        #endregion

    }
}
